package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.os.Handler;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.phone.R;
import com.lxj.xpopup.core.AttachPopupView;

public class AttachListDialog extends AttachPopupView {

    private DialogClickListener mDialogClickListener;
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private Handler mHandler;
    public AttachListDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_attach_recycler_view;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        mRecyclerView = findViewById(R.id.recycler_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(linearLayoutManager);
        if(mAdapter != null){
            mRecyclerView.setAdapter(mAdapter);
        }
    }

    public void setAdapter(RecyclerView.Adapter adapter){
        mAdapter = adapter;
        if(mRecyclerView != null){
            mRecyclerView.setAdapter(adapter);
        }
    }

    public RecyclerView.Adapter getAdapter() {
        return mAdapter;
    }

    public void scrollTo(int position) {
        if(mRecyclerView != null){
            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
            int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
            int newPosition;
            if(firstVisibleItemPosition >= position){
                newPosition = position - 1;
                newPosition = newPosition < 0 ? 0 : newPosition;
            }else{
                newPosition = position + 1;
                newPosition = newPosition > mAdapter.getItemCount() - 1 ? mAdapter.getItemCount() - 1 : newPosition;
            }
            mRecyclerView.smoothScrollToPosition(newPosition);
        }
    }

    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }

    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    protected int getPopupHeight() {
        return 0;
    }

    @Override
    protected void doAfterShow() {
        super.doAfterShow();
        if(mHandler == null){
            mHandler = new Handler();
        }
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mRecyclerView.setScrollBarSize(getResources().getDimensionPixelSize(R.dimen.dp_4));
                mRecyclerView.scrollBy(0, 1);
            }
        }, 300);
    }

    @Override
    protected void beforeDismiss() {
        super.doAfterDismiss();
        mRecyclerView.setScrollBarSize(0);
        mRecyclerView.scrollBy(0, -1);
    }

    public void setDialogClickListener(@Nullable DialogClickListener listener) {
        mDialogClickListener = listener;
    }

    public interface DialogClickListener {
        void onConfirmClick();
    }
}
